var $table = $("#sysUserTab");

$(function () {

    initTable();
    //查询事件
    $("#queryUserBtn").click(function () {
        var userName = $('#salesmanName').val();
        var comnumNum = $('#salesmanComnum').val();
        //使用post
        $table.bootstrapTable('refresh', {
            silent: false,
            url: '../user/getAllSalesmanUser.htm',
            query:{salesmanName:userName, salesmanComnum:comnumNum}
        });
    });

    $("#addBut").click(function () {
        initUserEdit(null);
    });
    
    //获取公司名称
    var comnum = $("#comnum").val();
    //如果公司名称不为nul和不为undefined，则加载信息
    if(!comnum && typeof(comnum) != 'undefined'){
    	$.ajax({
            url: "../company/getCompanyNumAndName.htm",
            cache: false,
            type: "get",
            dataType: "json",
            success: function (result) {
                if (result.status == 0) {
                	var data = result.data;
                	addOptions(data, "#comnum");
                    return;
                }
            }
        });
    }

});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        url: '../user/getAllSalesmanUser.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "编号",
                field: "salesmanNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "真实姓名",
                field: "salesmanRealName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "用户名",
                field: "salesmanName",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "入职时间",
            	field: "salesmanJoinTime",
            	align: "center",
            	valign: "middle"
            },
            {
                title: "所属公司",
                field: "companyDto.comnumName",
                align: 'center',
                valign: 'middle'
            },
            {
                title:"联系方式",
                field:"salesmanPhone",
                align: 'center',
                valign: 'middle'
            },
            {
            	title:"目标业绩(元)",
            	field:"salesmanGoal",
            	align: 'center',
            	valign: 'middle'
            },
            {
            	title:"奖金(元)",
            	field:"salesmanBonus",
            	align: 'center',
            	valign: 'middle'
            },
            {
            	title:"业务员类型",
            	field:"salesmanType",
            	align: 'center',
            	formatter: function(value, row, index){
            		var goal = "";
            		if(row.salesmanType == 1){
            			goal = "开单";
            		}else if(row.salesmanType == 2){
            			goal = "库管";
            		}
            		return goal;
            	}
            },
            {
            	title:"用户角色",
            	field:"roleDto.descr",
            	align: 'center',
            	valign: 'middle'
            },
            {
            	title:"状态",
            	field:"salesmanIsDelete",
            	align: 'center',
            	formatter: function(value, row, index){
            		var state = "";
            		if(row.salesmanIsDelete == 0){
            			state = "启用";
            		}else if(row.salesmanIsDelete == 1){
            			state = "禁用";
            		}
            		return goal;
            	}
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var e = '<button class="btn btn-xs btn-info" type="button" onclick="initUserEdit(\'' + row.id + '\',\'' + row.salesmanComnum + '\')"><i class="fa fa-edit"></i> 编辑</button> ';
                    var d = '';
                    if (row.salesmanIsDelete == 0){
                        d = '<button class="btn btn-xs btn-danger" type="button" onclick="deleteSysUser(\'' + row.id + '\',\'' + row.name + '\',\'' + 1 + '\')"><i class="fa fa-trash-o"></i> 禁用 </button> ';
                    }else if(row.salesmanIsDelete == 1){
                        d = '<button class="btn btn-xs btn-warning" type="button" onclick="deleteSysUser(\'' + row.id + '\',\'' + row.name + '\',\'' + 0 + '\')"><i class="fa fa-trash-o"></i> 启用 </button> ';
                    }
                    return e + d;
                }
            }
        ]
    });
}

function initUserEdit(id){
    var title;
    if(id){
        title = '编辑用户';
    }else{
        title = '新增用户';
    }
    layer.open({
        type: 2,
        title: title,
        maxmin: false,
        shadeClose: false, //点击遮罩关闭层
        area : ['100px' , '1200px'],
        content: '../user/edit_sysuser.htm?id='+id + "&comnum=" + salesmanComnum,
        success: function(layero, index){
        }
    });
}

function deleteSysUser(userId, name,state) {
    layer.confirm('你确定要修改【' + name + '】用户状态吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "/doublesys/user/deleteSysUser.htm",
            cache: false,
            type: "get",
            dataType: "json",
            data: {'id': userId,'state':state},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '/doublesys/user/getAllSysUser.htm?level=1'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}


//comnum的下拉列表  追加到option中
function addOptions(data, selectId){
	for(var i=0; i<data.length; i++){
		$(selectId).append('<option value="' + data[i].comnumNum + '">' + data[i].comnumName + '</option>');
	}
}